<template>
  <header class="header">
    <div class="container header-container">
      <!-- Logo -->
      <div class="logo-container">
        <NuxtLink to="/" class="logo">
          <img src="https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web/6c61ae65d1c41ae8221a670fa32d05aa.svg" alt="掘金">
        </NuxtLink>
      </div>

      <!-- Navigation -->
      <nav class="nav-container">
        <ul class="nav-list">
          <li v-for="item in navItems" :key="item.path" class="nav-item">
            <NuxtLink :to="item.path" class="nav-link" :class="{ active: currentPath === item.path }">
              {{ item.label }}
            </NuxtLink>
          </li>
        </ul>
      </nav>

      <!-- Search -->
      <div class="search-container">
        <el-input
          v-model="searchQuery"
          placeholder="搜索文章"
          prefix-icon="Search"
          clearable
          @keyup.enter="handleSearch"
        />
      </div>

      <!-- User actions -->
      <div class="user-actions">
        <template v-if="isLoggedIn">
          <el-dropdown trigger="click">
            <div class="user-avatar">
              <el-avatar :size="32" src="https://p3-passport.byteimg.com/img/user-avatar/3e4fc62a15247f13.png~100x100.awebp" />
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>我的主页</el-dropdown-item>
                <el-dropdown-item>写文章</el-dropdown-item>
                <el-dropdown-item>草稿箱</el-dropdown-item>
                <el-dropdown-item divided>设置</el-dropdown-item>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <el-button type="primary" @click="showLoginModal = true">登录</el-button>
        </template>
      </div>
    </div>
  </header>

  <!-- Login Modal -->
  <el-dialog
    v-model="showLoginModal"
    title="登录掘金"
    width="400px"
    destroy-on-close
  >
    <el-form>
      <el-form-item>
        <el-input v-model="loginForm.username" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
      </el-form-item>
      <div class="login-options">
        <span>没有账号？<a href="#" @click.prevent="showRegisterForm = true">注册</a></span>
        <span><a href="#">忘记密码</a></span>
      </div>
    </el-form>
  </el-dialog>
</template>

<script setup>

import { useRoute } from 'vue-router'
import {getToken, removeToken} from "~/utils/auth.js";
const { $apiClient } = useNuxtApp();

// Navigation items
const navItems = [
  { label: '首页', path: '/' },
 /* { label: '沸点', path: '/pins' },
  { label: '课程', path: '/courses' },
  { label: '直播', path: '/lives' },
  { label: '活动', path: '/events' },
  { label: '竞赛', path: '/challenge' },
  { label: '商城', path: '/shop' }*/
]

// Current route
const route = useRoute()
const currentPath = computed(() => route.path)

// Search functionality
const searchQuery = ref('')
const handleSearch = () => {
  if (searchQuery.value.trim()) {
    console.log('Searching for:', searchQuery.value)
    // Implement search functionality
  }
}

// Auth state (mocked)
const isLoggedIn = ref(false)
const showLoginModal = ref(false)
const showRegisterForm = ref(false)
const loginForm = ref({
  username: '',
  password: ''
})

// Login/Logout handlers
const handleLogin = () => {
  // Mock login
  if (loginForm.value.username && loginForm.value.password) {
    $apiClient.post('/login', loginForm.value) // 替换为你的API端点
        .then(response => {
          if (response.code === 200) {
            setToken(response.token)
            isLoggedIn.value = true
            showLoginModal.value = false
          }

        })
        .catch(error => {
          console.error('Error submitting form:', error);
        });

    // // Reset form
    // loginForm.value = {
    //   phone: '',
    //   password: ''
    // }
  }
}

onMounted(() => {
  // 检查 cookie 中是否存在 token
  const token = getToken();
  isLoggedIn.value = !!token;
});

const handleLogout = () => {
  removeToken()
  isLoggedIn.value = false
}
</script>

<style lang="scss" scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: $header-height;
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  z-index: $z-index-header;
}

.header-container {
  display: flex;
  align-items: center;
  height: 100%;
  justify-content: space-between;
}

.logo-container {
  margin-right: $spacing-lg;

  .logo {
    display: block;

    img {
      height: 22px;
      vertical-align: middle;
    }
  }
}

.nav-container {
  flex: 1;

  .nav-list {
    display: flex;
    list-style: none;

    .nav-item {
      margin-right: $spacing-md;

      .nav-link {
        display: block;
        padding: $spacing-xs $spacing-sm;
        font-size: 14px;
        color: $text-regular;
        transition: color 0.2s ease;

        &:hover, &.active {
          color: $primary-color;
        }
      }
    }
  }
}

.search-container {
  width: 240px;
  margin: 0 $spacing-lg;
}

.user-actions {
  display: flex;
  align-items: center;

  .user-avatar {
    cursor: pointer;
  }
}

.login-options {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  margin-top: $spacing-sm;

  a {
    color: $primary-color;

    &:hover {
      text-decoration: underline;
    }
  }
}

@media (max-width: $breakpoint-lg) {
  .nav-container {
    .nav-list {
      .nav-item:nth-child(n+5) {
        display: none;
      }
    }
  }

  .search-container {
    width: 180px;
  }
}

@media (max-width: $breakpoint-md) {
  .nav-container {
    .nav-list {
      .nav-item:nth-child(n+4) {
        display: none;
      }
    }
  }

  .search-container {
    width: 120px;
    margin: 0 $spacing-sm;
  }
}
</style>
